<?php
/*

Plugin Name: Alertas Widget
Plugin URI: http://rosarioaltoque.com.ar
Description: Un widget para mostrar las alertas
Author: Pampacom CDT
Author URI: http://pampacomcdt.com.ar
Version: 0.4
*/
add_action("widgets_init", array('Widget_Alertas', 'register'));
add_action('wp_print_scripts', 'add_widget_scripts');

function add_widget_scripts () {
  wp_enqueue_script( 'jcarousel-for-widgets-js', get_template_directory_uri(). '/js/jquery.jcarousel.js', array( 'jquery' ) );
  wp_enqueue_script( 'maps-google-for-widgets-js', 'http://maps.google.com/maps/api/js?sensor=true', array( 'jquery' ) );
}

class Widget_Alertas extends WP_Widget {
	function Widget_Alertas() {
		$widget_ops = array( 'classname' => 'Widget_Alertas');
		$control_ops = array( 'width' => 850, 'height' => 350, 'id_base' => 'alertas-widget' );
		$this->WP_Widget( 'alertas-widget', __('Widget Alertas', 'a'), $widget_ops, $control_ops );
	}
	function control(){
    	echo 'I am a control panel';
	}
	function widget($args){
		extract( $args );
		$title = "Al toque";
		
    	echo $args['before_widget'];
	    echo $args['before_title'] . $title . $args['after_title'];

      //echo '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>';
	    
      //echo '<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>';
	    
      echo '<script type="text/javascript">';
	    echo 'function initialize() {';
	    echo 'var latlng = new google.maps.LatLng(-32.949911, -60.665817);';
	    echo 'var myOptions = {';
	    echo 'zoom: 11,';
	    echo 'center: latlng,';
	    echo 'mapTypeId: google.maps.MapTypeId.ROADMAP';
	    echo '};';
	    echo 'var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);';

	    if ( have_posts() ) : while ( have_posts() ) : the_post();
	    	$id = get_the_id();
	    	$lat = get_post_meta($id, '_wp_geo_latitude', true);
	    	$lon = get_post_meta($id, '_wp_geo_longitude', true);
	    	if ($lat && $lon) {
		    	$category = get_the_category();
		    	echo 'var image = "/wp-content/themes/rcd/images/'.$category[0]->category_nicename.'.png";';
	    		echo 'var myLatLng = new google.maps.LatLng('.$lat.', '.$lon.');';
	    		echo 'var beachMarker = new google.maps.Marker({';
		    	echo 'position: myLatLng,';
		    	echo 'title: "'.get_the_title().'",';
		    	echo 'map: map,';
	    		echo 'icon: image';
	    		echo '});';
	    	}
	    endwhile;
	    endif;
	    
	    echo '}';
	    echo '</script>';

	    echo '<div id="map_canvas" class="alertas-widget"></div>';
	    echo '<script>jQuery(document).ready(function(){initialize()})</script>';
	    
	    echo $args['after_widget'];
	}
	function register(){
		register_sidebar_widget('Alertas', array('Widget_Alertas', 'widget'));
		register_widget_control('Alertas', array('Widget_Alertas', 'control'));
	}
}

?>
